package com.fatdetection.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fatdetection.demo.bean.Consult;
import com.fatdetection.demo.service.ConsultService;
import com.fatdetection.demo.util.RstMsgUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/consult")
public class ConsultController {
    private static Pattern p_image=Pattern.compile("<img.*src\\s*=\\s*(.*?)[^>]*?>",Pattern.CASE_INSENSITIVE);
    private static Pattern r_image=Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)");
    @Autowired
    ConsultService consultService;

    @ApiOperation(value = "分页获取数据", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前第几页", required = true),
            @ApiImplicitParam(name = "size", value = "每一页的条目数", required = true)
    })
    @GetMapping("/selectPage")
    public Object selectPage(Integer current, Integer size){
        //<img src="https:\/\/dfzximg01.dftoutiao.com\/news\/20210901\/20210901155543_cbfff9b37523ff8449ff03dc20808739_1.jpeg" style="max-width:100%;" contenteditable="false"/>
        Page<Consult> consultPage = consultService.selectPage(current, size);
        if(consultPage == null){
            return RstMsgUtils.error("无数据或获取失败");
        }
        List<Consult> records = consultPage.getRecords();
        Map<String, Object> rst = new HashMap<>();
        List<Map<String, Object>> datalist = new ArrayList<>();
        for (Consult record : records) {
            Map<String, Object> map = new HashMap<>();
            String content = record.getContent();
            map.put("id", record.getId());
            map.put("author", record.getAuthor());
            map.put("subject", record.getSubject());
            map.put("content", record.getContent());
            map.put("createTime", record.getCreateTime());
            map.put("updateTime", record.getUpdateTime());
            List<String> imgStr = getImgStr(content);
            if(imgStr.size() == 0){
                map.put("cover", "暂无封面图片");
            } else {
                map.put("cover", imgStr.get(0));
            }
            datalist.add(map);
        }
        rst.put("dataList", datalist);
        rst.put("current", consultPage.getCurrent());
        rst.put("total", consultPage.getTotal());
        rst.put("size", consultPage.getSize());
        return RstMsgUtils.success(null, rst);
    }

    @ApiOperation(value = "添加", notes = "时间不需要传递，其他都要")
    @PostMapping("/insert")
    public Object insert(Consult data){
        boolean status = consultService.insert(data);
        if(status){
            return RstMsgUtils.success(null, "");
        }
        return RstMsgUtils.error("失败咯");
    }

    @ApiOperation(value = "删除", notes = "")
    @PostMapping("/del")
    @ApiImplicitParam(name = "id", value = "id", required = true)
    public Object del(Integer id){
        boolean status = consultService.del(id);
        if(status){
            return RstMsgUtils.success(null, "");
        }
        return RstMsgUtils.error("失败咯");
    }
    @ApiOperation(value = "修改", notes = "时间不用传递，其他都要特别是id")
    @PostMapping("/update")
    public Object update(Consult data){
        boolean status = consultService.update(data);
        if(status){
            return RstMsgUtils.success(null, "");
        }
        return RstMsgUtils.error("失败咯");
    }
    @ApiOperation(value = "查询一条记录", notes = "")
    @GetMapping("/getOne/{key}/{val}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "键", required = true),
            @ApiImplicitParam(name = "val", value = "值", required = true)
    })
    public Object getOne(@PathVariable String key, @PathVariable String val){
        Consult data = consultService.getOne(key, val);
        if(data != null){
            return RstMsgUtils.success(null, data);
        }
        return RstMsgUtils.error("失败咯");
    }
    @ApiOperation(value = "查询所有", notes = "")
    @GetMapping("/getAll")
    public Object getAll(){
        List<Consult> all = consultService.getAll();
        if(all != null){
            return RstMsgUtils.success(null, all);
        }
        return RstMsgUtils.error("失败咯");
    }

    public List<String> getImgStr(String htmlStr) {
        List<String> list = new ArrayList<>();
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        Matcher m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            // 得到<img />数据
            String img = m_image.group();
            System.out.println(img);
            // 匹配<img>中的src数据
            Matcher m = r_image.matcher(img);
            while (m.find()) {
                list.add(m.group(1));
            }
        }
        return list;
    }
}
